using AForge.Video;
using Declarations;
using Declarations.Media;
using Declarations.Players;
using Implementation;
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace SilverSea.Video.Tools
{
	public class VLC_FileVideoSource : IVideoSource
	{
		private IMediaPlayerFactory imediaPlayerFactory_0;
		private IVideoPlayer ivideoPlayer_0;
		private IMedia imedia_0;
		private IMemoryRenderer imemoryRenderer_0;
		private string string_0;
		private string string_1;
		private string string_2;
		private System.Drawing.Size size_0 = new System.Drawing.Size(0, 0);
		private int int_0;
		private int int_1;
		private int int_2;
		private bool bool_0 = true;
		private int int_3 = 10000;
		private DateTime dateTime_0 = DateTime.Now;
		private object object_0 = new object();
		private Declarations.NewFrameEventHandler newFrameEventHandler_0;
        private AForge.Video.NewFrameEventHandler newFrameEventHandler_1;
		private VideoSourceErrorEventHandler videoSourceErrorEventHandler_0;
		private PlayingFinishedEventHandler playingFinishedEventHandler_0;
        public event AForge.Video.NewFrameEventHandler NewFrame;
        //{
        //    add
        //    {
        //        Declarations.NewFrameEventHandler newFrameEventHandler = this.newFrameEventHandler_1;
        //        Declarations.NewFrameEventHandler newFrameEventHandler2;
        //        do
        //        {
        //            newFrameEventHandler2 = newFrameEventHandler;
        //            Declarations.NewFrameEventHandler value2 = (Declarations.NewFrameEventHandler)Delegate.Combine(newFrameEventHandler2, value);
        //            newFrameEventHandler = Interlocked.CompareExchange<Declarations.NewFrameEventHandler>(ref this.newFrameEventHandler_1, value2, newFrameEventHandler2);
        //        }
        //        while (newFrameEventHandler != newFrameEventHandler2);
        //    }
        //    remove
        //    {
        //        Declarations.NewFrameEventHandler newFrameEventHandler = this.newFrameEventHandler_1;
        //        Declarations.NewFrameEventHandler newFrameEventHandler2;
        //        do
        //        {
        //            newFrameEventHandler2 = newFrameEventHandler;
        //            Declarations.NewFrameEventHandler value2 = (Declarations.NewFrameEventHandler)Delegate.Remove(newFrameEventHandler2, value);
        //            newFrameEventHandler = Interlocked.CompareExchange<Declarations.NewFrameEventHandler>(ref this.newFrameEventHandler_1, value2, newFrameEventHandler2);
        //        }
        //        while (newFrameEventHandler != newFrameEventHandler2);
        //    }
        //}
		public event VideoSourceErrorEventHandler VideoSourceError;
        //{
        //    add
        //    {
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
        //        do
        //        {
        //            videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
        //            VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Combine(videoSourceErrorEventHandler2, value);
        //            videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
        //        }
        //        while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
        //    }
        //    remove
        //    {
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
        //        do
        //        {
        //            videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
        //            VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Remove(videoSourceErrorEventHandler2, value);
        //            videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
        //        }
        //        while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
        //    }
        //}
		public event PlayingFinishedEventHandler PlayingFinished;
        //{
        //    add
        //    {
        //        PlayingFinishedEventHandler playingFinishedEventHandler = this.playingFinishedEventHandler_0;
        //        PlayingFinishedEventHandler playingFinishedEventHandler2;
        //        do
        //        {
        //            playingFinishedEventHandler2 = playingFinishedEventHandler;
        //            PlayingFinishedEventHandler value2 = (PlayingFinishedEventHandler)Delegate.Combine(playingFinishedEventHandler2, value);
        //            playingFinishedEventHandler = Interlocked.CompareExchange<PlayingFinishedEventHandler>(ref this.playingFinishedEventHandler_0, value2, playingFinishedEventHandler2);
        //        }
        //        while (playingFinishedEventHandler != playingFinishedEventHandler2);
        //    }
        //    remove
        //    {
        //        PlayingFinishedEventHandler playingFinishedEventHandler = this.playingFinishedEventHandler_0;
        //        PlayingFinishedEventHandler playingFinishedEventHandler2;
        //        do
        //        {
        //            playingFinishedEventHandler2 = playingFinishedEventHandler;
        //            PlayingFinishedEventHandler value2 = (PlayingFinishedEventHandler)Delegate.Remove(playingFinishedEventHandler2, value);
        //            playingFinishedEventHandler = Interlocked.CompareExchange<PlayingFinishedEventHandler>(ref this.playingFinishedEventHandler_0, value2, playingFinishedEventHandler2);
        //        }
        //        while (playingFinishedEventHandler != playingFinishedEventHandler2);
        //    }
        //}
		public bool SeparateConnectionGroup
		{
			get
			{
				return this.bool_0;
			}
			set
			{
				this.bool_0 = value;
			}
		}
		public string Source
		{
			get
			{
				return this.string_0;
			}
			set
			{
				this.string_0 = value;
			}
		}
		public string Login
		{
			get
			{
				return this.string_1;
			}
			set
			{
				this.string_1 = value;
			}
		}
		public string Password
		{
			get
			{
				return this.string_2;
			}
			set
			{
				this.string_2 = value;
			}
		}
		public System.Drawing.Size DesiredFrameSize
		{
			get
			{
				return this.size_0;
			}
			set
			{
				this.size_0 = value;
			}
		}
		public int DesiredFrameRate
		{
			get
			{
				return this.int_0;
			}
			set
			{
				this.int_0 = value;
			}
		}
		public int FramesReceived
		{
			get
			{
				int result = this.int_1;
				this.int_1 = 0;
				return result;
			}
			set
			{
				this.int_1 = value;
			}
		}
		public int BytesReceived
		{
			get
			{
				int result = this.int_2;
				this.int_2 = 0;
				return result;
			}
			set
			{
				this.int_2 = value;
			}
		}
		public int RequestTimeout
		{
			get
			{
				return this.int_3;
			}
			set
			{
				this.int_3 = value;
			}
		}
		public bool IsRunning
		{
			get
			{
				bool result = false;
				lock (this.object_0)
				{
					if (this.ivideoPlayer_0 != null)
					{
                        result = this.ivideoPlayer_0.IsPlaying;
					}
				}
				return result;
			}
		}
		public VLC_FileVideoSource(string source)
		{
			this.string_0 = source;
		}
		public void Start()
		{
			try
			{
				lock (this.object_0)
				{
					this.imediaPlayerFactory_0 = new MediaPlayerFactory();
					this.ivideoPlayer_0 = this.imediaPlayerFactory_0.CreatePlayer<IVideoPlayer>();
					int width = 640;
					int height = 480;
					if (this.size_0.Width > 0 && this.size_0.Height > 0)
					{
						width = this.size_0.Width;
						height = this.size_0.Height;
					}
					this.imedia_0 = this.imediaPlayerFactory_0.CreateMedia<IMedia>(this.string_0, new string[0]);
                    this.imemoryRenderer_0 = this.ivideoPlayer_0.CustomRenderer;
					//this.imemoryRenderer_0.SetFormat(new BitmapFormat(width, height, 2));
                    this.imemoryRenderer_0.SetFormat(new BitmapFormat(width, height, ChromaType.RV24));
                    this.newFrameEventHandler_0 = new Declarations.NewFrameEventHandler(this.method_0);
					this.imemoryRenderer_0.SetCallback(this.newFrameEventHandler_0);
					this.ivideoPlayer_0.Open(this.imedia_0);
					this.imedia_0.Parse(true);
					this.ivideoPlayer_0.Play();
				}
			}
			catch (Exception ex)
			{
				if (this.videoSourceErrorEventHandler_0 != null)
				{
					this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs(ex.Message));
				}
			}
		}
		private void method_0(System.Drawing.Bitmap bitmap_0)
		{
			this.int_1++;
			this.dateTime_0 = DateTime.Now;
			if (this.newFrameEventHandler_1 != null)
			{
				this.newFrameEventHandler_1.Invoke(this, new NewFrameEventArgs(bitmap_0));
				bitmap_0.Dispose();
				bitmap_0 = null;
			}
		}
		public void SignalToStop()
		{
			lock (this.object_0)
			{
				if (this.ivideoPlayer_0 != null && this.IsRunning)
				{
					this.ivideoPlayer_0.Stop();
				}
				Application.DoEvents();
			}
		}
		public void WaitForStop()
		{
		}
		public void Stop()
		{
			try
			{
				lock (this.object_0)
				{
					if (this.imemoryRenderer_0 != null)
					{
						this.imemoryRenderer_0 = null;
					}
					Application.DoEvents();
					if (this.imedia_0 != null)
					{
						this.imedia_0.Dispose();
						this.imedia_0 = null;
					}
					Application.DoEvents();
					if (this.ivideoPlayer_0 != null)
					{
						this.ivideoPlayer_0.Dispose();
						this.ivideoPlayer_0 = null;
					}
					Application.DoEvents();
					if (this.imediaPlayerFactory_0 != null)
					{
						this.imediaPlayerFactory_0.Dispose();
						this.imediaPlayerFactory_0 = null;
					}
					Application.DoEvents();
                    if (this.playingFinishedEventHandler_0 != null)
					{
                        this.playingFinishedEventHandler_0.Invoke(this, ReasonToFinishPlaying.StoppedByUser);
					}
				}
			}
			catch (Exception ex)
			{
				if (this.videoSourceErrorEventHandler_0 != null)
				{
					this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs(ex.Message));
				}
			}
		}
	}
}
